<!DOCTYPE html>
<html lang="en" class="no-js" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>部门管理</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
<!--顶部搜索框-->
<form class="layui-form" action="/dictionary/queryAll" style="margin-top: 10px">
    <div class="layui-inline">
        <label class="layui-form-label">ID:</label>
        <div class="layui-input-inline">
            <input type="text" name="dictionaryId" autocomplete="off" placeholder="ID" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">修改人:</label>
        <div class="layui-input-inline">
            <input type="text" name="updateBy" autocomplete="off" placeholder="修改人" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">创建人:</label>
        <div class="layui-input-inline">
            <input type="text" name="createBy" autocomplete="off" placeholder="创建人" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn" lay-submit lay-filter="searchFilter">搜索</button>
            <button type="reset" class="layui-btn layui-btn-normal" id="myButton">重置</button>
        </div>
    </div>
</form>
<!--部门列表显示-->
<table id="dictionaryTable" lay-filter="dictionaryFilter"></table>
<!--部门更新form-->
<form id="dictionaryUpdateForm" class="layui-form" lay-filter="dictionaryUpdateFilter" style="display: none"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <input hidden name="dictionaryId">
    <div class="layui-form-item">
        <label class="layui-form-label">字典类型</label>
        <div class="layui-input-block">
            <select name="typeId" lay-verify="required" placeholder="请选择字典类型" id="update_typeId">
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">字典名称</label>
        <div class="layui-input-block">
            <input type="text" name="dictionaryName" lay-verify="required" placeholder="" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea type="text" name="remark" placeholder="" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="dictionaryUpdateFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-normal">重置</button>
        </div>
    </div>
</form>
<!--部门添加form-->
<form id="dictionaryAddForm" class="layui-form" style="display: none"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <div class="layui-form-item">
        <label class="layui-form-label">字典类型</label>
        <div class="layui-input-block">
            <select name="typeId" lay-verify="required" placeholder="请选择字典类型" id="select_typeId">
                <option value="">请选择商标类型</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">字典名称</label>
        <div class="layui-input-block">
            <input type="text" name="dictionaryName" lay-verify="required" placeholder="" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea type="text" name="remark" placeholder="" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="dictionaryAddFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-normal">重置</button>
        </div>
    </div>
</form>
<!--行编辑工具栏-->
<script type="text/html" id="barRowEdit">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!--头部工具栏-->
<script type="text/html" id="toolbarHead">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="delete">删除</button>
        <button class="layui-btn layui-btn-sm" lay-event="update">编辑</button>
    </div>
</script>
<!--JavaScript代码区域-->
<script src="/layui/layui.js"></script>
<!--列表信息显示-->
<script>
    layui.use(['table','layer','form','jquery'],function(){
        let table = layui.table;//数据表格模块
        let layer = layui.layer;//弹出层模块
        let form = layui.form;//表单模块
        let $ = layui.jquery;

        $(function () {
            //获取user表的数据，用于代理人的添加和更新
            $.ajax({
                url:"/dictionaryType/queryAll1",
                type:"GET",
                dataType:"json",
                success:function (resp) {
                    if(resp.code==0){
                        for(let i = 0;i < resp.data.length;i++){
                            $("#select_typeId").append("<option value='" + resp.data[i].typeId + "'>" + resp.data[i].typeName + "</option>");
                            $("#update_typeId").append("<option value='" + resp.data[i].typeId + "'>" + resp.data[i].typeName + "</option>")
                        }
                    }
                },
                error:function (e) {
                    layer.msg("请求失败");
                }
            })
        })
        //部门列表
        let dictionaryTable = table.render({
            elem: '#dictionaryTable' //table标签的id
            ,toolbar: '#toolbarHead'//开启头部工具栏，并为其绑定左侧模板
            ,url: '/dictionary/queryAll' //数据接口
            ,page: true //开启分页，默认向后台发两个参数page和limit
            ,cellMinWidth: 50//自适应宽度，最短为80
            ,cols: [
                [ //表头 两个中括号不能在同一行，与thymeleaf语法冲突
                    {type: 'checkbox', fixed: 'left'}
                    , {field: 'dictionaryId', title: '字典ID', width: 100, sort: true}
                    , {field: 'typeId', title: '类型ID', width: 100, hide: true}
                    , {field: 'typeName', title: '类型名称', width: 120}
                    , {field: 'dictionaryName', title: '字典名称', width: 300}
                    , {field: 'createBy', title: '创建人', width: 100}
                    , {field: 'createTime', title: '创建时间', width: 200}
                    , {field: 'updateBy', title: '修改人', width: 100}
                    , {field: 'updateTime', title: '修改时间', width: 200}
                    , {field: 'remark', title: '备注', width: 200}
                    , {fixed: 'right', title:'操作',align:'center', toolbar: '#barRowEdit', width: 120}
                ]
            ]
        });
        //行编辑工具栏监听事件
        table.on('tool(dictionaryFilter)', function(obj){
            //获取当前行数据
            let data = obj.data;
            //判断是删除还是编辑
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    //向后台发送异步请求
                    $.ajax({
                        url:"/dictionary/deleteById",
                        type:"DELETE",
                        data:{dictionaryId:data.dictionaryId},
                        dataType:"json",
                        success:function (resp) {
                            if(resp.code==0){
                                layer.msg(resp.msg);
                                //重新加载数据
                                dictionaryTable.reload({
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                })
                            }
                        },
                        error:function (e) {
                            layer.msg("请求失败");
                        }
                    });
                });
            } else if(obj.event === 'edit'){
                //表单赋值，取值 这里是赋值
                form.val("dictionaryUpdateFilter",data);
                //打开弹出层
                layer.open({
                    type: 1,//指页面层
                    area:['500px', '350px'],
                    content: $('#dictionaryUpdateForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });
            }
        });
        //头部工具栏监听事件
        table.on('toolbar(dictionaryFilter)', function(obj){
            let checkStatus = table.checkStatus(obj.config.id);
            let  checkData = checkStatus.data;
            switch(obj.event){
                case 'add':
                    //重置form表单
                    document.getElementById("dictionaryAddForm").reset();
                    layer.open({
                        type: 1,
                        area:['400px', '350px'],
                        content: $('#dictionaryAddForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;
                case 'delete':
                    //判断是否选中行
                    if(checkData.length==0){
                        layer.msg('请至少选择一行数据');
                        return;
                    }
                    layer.confirm('真的删除行么', function(index) {
                        //获取所有选中行id
                        let dictionaryIds = "";
                        for (let i = 0; i < checkData.length; i++) {
                            dictionaryIds += checkData[i].dictionaryId + ",";
                        };
                        dictionaryIds = dictionaryIds.substr(0, dictionaryIds.length - 1);
                        layer.msg(dictionaryIds);
                        //调用批量删除
                        $.ajax({
                            url: "/dictionary/deleteByIds",
                            type: "DELETE",
                            data: {dictionaryIds: dictionaryIds},
                            dataType: "json",
                            success: function (resp) {
                                if (resp.code == 0) {
                                    layer.msg(resp.msg);
                                    //重新加载数据
                                    dictionaryTable.reload();
                                }
                            },
                            error: function (e) {
                                layer.msg("请求失败");
                            }
                        });
                    });
                    break;
                case 'update':

                    if(checkData.length!=1){
                        layer.msg('编辑只能选中一行');
                        return;
                    }
                    form.val("dictionaryUpdateFilter",checkData[0]);
                    layer.open({
                        type: 1,
                        area:['400px', '350px'],
                        content: $('#dictionaryUpdateForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;
            };
        });
        //更新(编辑)的监听事件
        form.on('submit(dictionaryUpdateFilter)', function(data){
            layer.msg(JSON.stringify(data.field));
            $.ajax({
                url:"/dictionary/update",
                type:"PUT",
                data:data.field,
                dataType:"json",
                success:function (resp) {
                    if(resp.code==0){
                        layer.msg(resp.msg);
                        //关闭弹框
                        layer.closeAll('page');
                        //重新加载数据
                        dictionaryTable.reload();
                    }
                },
                error:function (e) {
                    layer.msg("请求失败");
                }
            });
            return false;
        });
        //添加监听事件
        form.on('submit(dictionaryAddFilter)', function(data){
            layer.msg(JSON.stringify(data.field));
            $.ajax({
                url:"/dictionary/insert",
                type:"POST",
                data:data.field,
                dataType:"json",
                success:function (resp) {
                    if(resp.code==0){
                        layer.msg(resp.msg);
                        //关闭弹框
                        layer.closeAll('page');
                        //重新加载数据
                        dictionaryTable.reload();
                    }
                },
                error:function (e) {
                    layer.msg("请求失败");
                }
            });
            return false;
        });
        //顶部搜索框监听事件
        form.on('submit(searchFilter)', function (data) {
            //执行重载
            dictionaryTable.reload({
                where: {
                    dictionaryId: data.field.dictionaryId,
                    updateBy: data.field.updateBy,
                    createBy: data.field.createBy
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //重置条件查询表单，刷新table数据
        $("#myButton").click(function () {
            dictionaryTable.reload({
                where: { //设定异步数据接口的额外参数，任意设
                    dictionaryId: ""
                    , updateBy: ""
                    , createBy: ""
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });
    });
</script>
</body>
</html>